package com.wwy.readwrite.config;

/**
 * @author 王伟羽
 * @program: readwrite-demo
 * @description:
 * @create: 2025/02/08
 */

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * dao层配置
 */
@Configuration
@MapperScan(basePackages = "com.example.masterslavedb.dao", sqlSessionFactoryRef = "sqlSessionFactoryBean")
public class SpringDaoConf {
    /**
     * 主库数据源
     * @return
     */
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "master.datasource")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 从库数据源
     * @return
     */
    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "slave.datasource")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 我们自己实现的动态数据源
     * @return
     */
    @Bean(name = "dynamicDataSource")
    public DynamicDataSource dynamicDataSource() {
        DynamicDataSource result = new DynamicDataSource();
        Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
        targetDataSources.put("master", masterDataSource());
        targetDataSources.put("slave", slaveDataSource());
        result.setTargetDataSources(targetDataSources);
        return result;
    }

    /**
     * 因为要在确定了sql语句之后才能确定选择哪个数据源，所以我们需要一个懒加载数据源
     * @return
     */
    @Bean(name = "dataSourceProxy")
    public LazyConnectionDataSourceProxy dataSourceProxy() {
        LazyConnectionDataSourceProxy result = new LazyConnectionDataSourceProxy();
        result.setTargetDataSource(dynamicDataSource());
        return result;
    }

    @Bean(name = "sqlSessionFactoryBean")
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
        SqlSessionFactoryBean result = new SqlSessionFactoryBean();
        result.setDataSource(dataSourceProxy());
        //设置entity包扫描路径
        result.setTypeAliasesPackage("com.wwy.readwrite.domain.po");
        //注入mybatis配置
        result.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
        //设置sql映射文件包扫描路径
        result.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return result;
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSourceProxy());
    }

}

